<?php 

class Route{

	static $_request = array(); 
	static $pathinfo = '/' ; 
	//解析路由 http://c1.com/index.php?m=default&c=controller&a=index
	//$_SERVER['QUERY_STRING'] = m=default&c=controller&a=index 
	//$_SERVER['SCRIPT_NAME'] = /index.php
	//REQUEST_URI = /index.php?m=default&c=controller&a=index
	//DOCUMENT_URI = /index.php 
	static function dispath(){
		self::$_request = array_merge(self::$_request , $_GET , $_POST) ; 

		if(isset(self::$_request['m'])){
			$module = self::$_request['m'];
		}else{
			$module = 'app';
		}

		if(isset(self::$_request['c'])){
			$controller = ucwords(self::$_request['c']) ;
		}else{
			$controller = 'index';
		}
		
		if(isset(self::$_request['a'])){
			$action = self::$_request['a'] ;
		}else{
			$action = 'index';
		}

		$file = ROOT_PATH . '/' . $module . '/controller/' . strtolower($controller) .  __EXT__ ;   

		if(!file_exists($file)){
			Throw new Exception('控制器不存在');
		}

		//定义名称 . 
		defined('MODULE_NAME') or define('MODULE_NAME' , $module) ;
		defined('CONTROLLER_NAME') or define('CONTROLLER_NAME' , $controller);
		defined('ACTION_NAME') or define('ACTION_NAME' , $action );

		//定义目录名称 
		defined('VIEW_PATH') or define('VIEW_PATH' , MODULE_NAME . '/' . 'view/' . ACTION_NAME . '/');
		defined('CONTROLLER_PATH') or define('CONTROLLER_PATH' , MODULE_NAME . '/' . 'controller/');
		defined('MODEL_PATH') or define('MODEL_PATH' , MODULE_NAME . '/' . 'model/');


   		defined('RUNTIME_PATH') or define('RUNTIME_PATH' , ROOT_PATH . 'runtime/' .MODULE_NAME . '/' ); 


		include_once $file ;
		$controller = new $controller();
		$controller->$action();
	}
}